package com.ddxz.demo.shiro.configuration;

import com.ddxz.demo.shiro.mapper.ShiroMapper;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/11/25
 */

@Configuration
public class ShiroConfigration {
    @Bean
    public Realm realm(ShiroMapper mapper) {
        return new DdxzRealm(mapper);
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(Realm realm) {
        return new DefaultWebSecurityManager(realm);
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager manager) {
        ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
        filterFactoryBean.setSecurityManager(manager);
        filterFactoryBean.getFilters().put("ddxz", new DdxzFilter());
        Map<String, String> authMap = new HashMap<>(1);
        authMap.put("/swagger-ui.html", "anon");
        authMap.put("/webjars/springfox-swagger-ui/**", "anon");
        authMap.put("/swagger-resources/**", "anon");
        authMap.put("/v2/api-docs", "anon");
        authMap.put("/login", "anon");
        authMap.put("/**", "ddxz");
        filterFactoryBean.setFilterChainDefinitionMap(authMap);

        return filterFactoryBean;
    }
}
